from binance.client import Client
from collections import defaultdict
import json
import time
from datetime import datetime

client = Client()

all_symbol = ['BTCUSDT','ETHUSDT']

# 获取订单簿数据
def get_capital_rate():
    for symbol in all_symbol:
        while(1):
            try:
                funding_rate = client.futures_funding_rate(symbol=symbol)
                # 将订单簿数据写入TXT文件，a追加模式
                current_time = datetime.now().strftime('%m_%d_%H')
                with open('./capital_rate/'+current_time+'_'+symbol+'.txt', 'a') as file:
                    file.write(json.dumps(funding_rate) + '\n')
                time.sleep(1)
                break
            except Exception as e:
                print("binance 获取数据失败 重新获取")

def main():
    while(1):
        get_capital_rate()
        time.sleep(10)

if __name__ == "__main__":
    main()

